package fun.easycode.jointblock.exception;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;

/**
 * 通用逻辑异常
 * @Author: xuzhen97
 * @Date:  2023/03/22
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class CheckException extends RuntimeException{

    /**
     * 通用逻辑错误码
     * @Author: xuzhen97
     * @Date:  2023/03/22
     */
    public final static Integer DEFAULT_CODE = 40001;

    /**
     * 错误码
     * @Author: xuzhen97
     * @Date:  2023/03/22
     */
    private final Integer code;
    /**
     * 错误信息
     * @Author: xuzhen97
     * @Date:  2023/03/22
     */
    private final String msg;

    public CheckException(String msg){
        super(msg);
        this.code = DEFAULT_CODE;
        this.msg = msg;
    }

    public CheckException(Integer code, String msg){
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    /**
     * 返回结果
     * @Author: xuzhen97
     * @Date:  2023/03/22
     */
    public Result result(){
        return new Result(code, msg);
    }

    public static Result toResult(Integer code, String msg){
        return new Result(code, msg);
    }

    public static Result toResult(String msg){
        return new Result(DEFAULT_CODE, msg);
    }

    @AllArgsConstructor
    @Getter
    public static class Result{
        private final Integer code;
        private final String msg;
    }
}
